package mongo;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.types.ObjectId;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class SchoolYearSemester {
	
    MongoDatabase mongoDatabase = null;
    MongoCollection<Document> SchoolYearSemester;
    
    public SchoolYearSemester(){
        try{
        	mongoDatabase = mongoConnet.getMongoDatabase();
        	SchoolYearSemester = mongoDatabase.getCollection("SchoolYearSemester");
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }
    }
    
    public  Document get(){
        try{
            FindIterable<Document> findIterable = SchoolYearSemester.find();
            MongoCursor<Document> mongoCursor = findIterable.iterator();
            if(mongoCursor.hasNext()){
            	return mongoCursor.next();
            }else{
            	return null;
            }
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            return null;
        }
    }
    
    
    public Boolean add(Document department){
    	try{
    		SchoolYearSemester.insertOne(department);
    		return true;
    	}catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            return false;
    	}
    }
    
    
    public boolean update(ObjectId id,Document document){
        try{
        	SchoolYearSemester.updateOne(Filters.eq("_id", id), new Document("$set",document));
            return true;
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            return false;
        }
    }
    

}
